// pages/user/money.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:3,

    showAddBank: 0,
    user_info:{},
    array: ['招商银行', '北京银行'],
    bankname: '',
    selectBank:{},
    page:1,
    lastpage:0,
    loading:0,
    moneyLog:[],
    myBanks:[],
  },
  bindPickerChange: function (e) {
    this.setData({ bankname: this.data.array[e.detail.value] });
    console.log(e);
  },
  doAddBank:function(){
    this.setData({ showAddBank: 0 });
  },
  selectBank: function(e){
    var selectBankIndex = e.currentTarget.id;
    this.setData({ selectBank: this.data.myBanks[selectBankIndex]});
    this.setViweType(4);

  },
  doWithDraw:function(){
   
    if (this.data.disabled==1){
      return false;
    }
    this.setData({ loadingPost: 1, disabled: 1 });
    var that = this;
    app.api("user/withDraw", {}, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.setData({ loadingPost: false, disabled: 0 });
        return;
      }
      that.onPullDownRefresh();
      that.setViweType(0);
      this.setData({ loadingPost: 0, disabled: 0 });

    }) 

   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var user_info = wx.getStorageSync('user_info');
    // this.setData({ user_info: user_info});
    // //console.log(options);
     this.getInfo();
     this.loadmoney();
    //我的银行卡 
   // this.getMyBanks();
  },
  getInfo:function(){
    var that = this;
    app.api("user/info", {}, function (res) {
      wx.stopPullDownRefresh();
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      var income=(res.data.income*0.99).toFixed(2);
      var fee = (res.data.income * 0.01).toFixed(2);
      that.setData({ fee: fee, income:income,user_info: res.data });
    })  
  },
//我的银行卡 
  getMyBanks:function(){
    var that = this;
    app.api("user/banks", {
      user_id: this.data.user_info.user_id,
    }, function (data) {
      if (data.code != 200) {
        app.alert(data.msg);
        return;
      }
      //console.log(data.data.name);
      that.setData({
        myBanks: data.data,
        selectBank: data.data ? data.data[0]:{},
      });

    })
  },
  //资金流水 
  loadmoney:function(reload=0){
    var that = this;
    var page = this.data.page;
    if (reload==1){
      this.setData({
        loading:0,
        page:1,
        lastpage:0,
        moneyLog:[],
      });
    }
    if (this.data.loading==1 || this.data.lastpage==1){
      //console.log(this.data);
      return ;
    }
    this.data.loading=1;
    app.api("money_log/get", {
      page: page,
      limit:10,
      order:'id desc'
    },function(res){
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length==0){
        that.data.lastpage=1;
      }
      that.data.page++;
      that.setData({
        moneyLog: that.data.moneyLog.concat(res.data.list),
      });
    })
  },
  showView:function(e){
    this.setViweType(e.currentTarget.dataset.id );
    console.log(e.currentTarget.dataset.id);
  },
  setViweType:function(id){
    this.setData({ type: id });
    var title ="我的总收入";
    // if(id<4){
    //   this.setData({ moneyLog: [], page: 1 });
    //   this.loadmoney();
    // }
    if(id==2){
      title="未结算的金额";
    }else if(id==3){
      title = "我的计划金";
    }else if(id ==4){
      title = "提现到微信零钱";
    }else if(id == 5){
      title = "银行卡管理";
    }else{
      title = "我的计划金";
    }
    wx.setNavigationBarTitle({
      title: title
    })
  },
  addBank: function (e) {
    console.log(e);
    this.setData({ showAddBank: e.currentTarget.dataset.id });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  unDoWithDraw:function(){
    this.setViweType(3);
    //setViweType(3)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getInfo();
    this.loadmoney(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadmoney();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})